package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.mapper.PermissionMapper;
import org.javaweb.cms_web.mapper.TeamMapper;
import org.javaweb.cms_web.model.Enum.PermissionTypes;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.model.UserPermission;
import org.javaweb.cms_web.service.TeamService;
import org.javaweb.cms_web.util.HostHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TeamAdminController {
    @Resource
    private TeamService teamService;
    @Resource
    private HostHolder hostHolder;
    @Resource
    private PermissionMapper permissionMapper;

    // 跳转到团队管理员总界面
    @GetMapping("/admin/teamadmin")
    public String showTeamAdminPage() {
        return "team-admin";  // 返回团队管理员界面页面
    }

    // 跳转到科研成果管理与发布界面
    @GetMapping("/admin/teamadmin/achievementmanagement")
    public String redirectToAchievementManagement() {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.SCIENTIFIC_RESEARCH_MANAGE.value)) {
            return "nopermission";
        }
        return "redirect:/admin/achievementmanagement";

    }

    // 跳转到用户管理界面
    @GetMapping("/admin/teamadmin/usermanagement")
    public String redirectToUserManagement() {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.USER_MANAGE.value)) {
            return "nopermission";
        }
        return "redirect:/admin/usermanagement";
    }

    // 跳转到文章管理界面
    @GetMapping("/admin/teamadmin/articlemanagement")
    public String redirectToArticleManagement() {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.ARTICLE_MANAGE.value)) {
            return "nopermission";
        }
        return "redirect:/admin/articlesManagement";
    }


    // 跳转到反馈模块界面
    @GetMapping("/admin/teamadmin/feedbackmanagement")
    public String redirectToFeedbackManagement() {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.ONLINE_COMMUNICATION.value)) {
            return "nopermission";
        }
        return "redirect:/admin/FeedbacksManagement";
    }

    //跳转到团队管理界面
    @GetMapping("/admin/teamadmin/teammanagement")
    public String redirectToTeamManagement(Model model) {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.TEAM_MANAGE.value)) {
            return "nopermission";
        }
        model.addAttribute("team", teamService.getTeamById(user.getTeamId()));
        return "/admin/teammanagement";  // 跳转到团队管理页面
    }
}

